package com.i2863.receipt.dao;

import com.i2863.receipt.entity.Activity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */
public interface ActivityRepository extends JpaRepository<Activity,Integer>,JpaSpecificationExecutor<Activity>{

    @Query(value="select * from d_activity where site_id = :siteId and(starttime<now() and endtime>now()) order by num desc",nativeQuery = true)
    List<Activity> findOneByNow(@Param("siteId")Integer siteId);

    @Modifying
    @Query("update Activity  set activityStatus = ?1 where id = ?2")
    void updateActivityStatus(Integer activityStatus,Integer id);



    @Query(value = "select count(*) from d_activity where site_id = :siteId",nativeQuery = true)
    Integer countBySiteId(@Param("siteId")Integer siteId);


    /**
     * 获得站点下最大的活动期数
     * @param siteId
     * @return
     */
    @Query(value = "select max(num) from d_activity where site_id = :siteId",nativeQuery = true)
    Integer getMaxActivityNum(@Param("siteId")Integer siteId);

    /**
     * 设置活动状态为结束（状态为3）
     * @param activityId
     */
    @Modifying
    @Query(value = "update d_activity set activity_status=3 where id=:id",nativeQuery = true)
    void finishActivity(@Param("id")Integer activityId);



}
